<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\Core\Lib;

/**
 * Maintains a separate array of keys to retain the array-like traits while at
 * the same time refraining from retrieving all of the values upon
 * initialisation.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
abstract class AbstractProxiedParameterHolder extends ParameterHolder {

  /**
   * @var array An array of keys.
   */
  protected $proxy = array();

  /**
   * {@inheritDoc}
   * The proxy array contains the keys of all the elements that can be retrieved
   * upon demand.
   */
  public function __construct($proxy = array()) {
    $this->proxy = $proxy;
    $this->position = current($this->proxy);
  }

  /**
   * {@inheritDoc}
   */
  public function count() {
    return count($this->proxy);
  }

  /**
   * {@inheritDoc}
   */
  public function next() {
    next($this->proxy);
    $this->position = current($this->proxy);
  }

  /**
   * {@inheritDoc}
   */
  public function offsetExists($offset) {
    return in_array($offset, $this->proxy, true);
  }

  /**
   * {@inheritDoc}
   */
  public function &offsetGet($offset) {
    $resp = null;
    if (!$this->offsetExists($offset)) {
      return $resp;
    }
    if (!parent::offsetExists($offset)) {
      parent::offsetSet($offset, $this->get($offset));
    }
    $resp = &$this->data[$offset];
    return $resp;
  }

  /**
   * {@inheritDoc}
   */
  public function offsetSet($offset, $value) {
    if (is_null($offset)) {
      $offset = count($this->proxy) ? (max($this->proxy) + 1) : 0;
    }
    parent::offsetSet($offset, $value);
    $this->set($offset, $value);
    if (!$this->offsetExists($offset)) {
      $this->proxy[] = $offset;
    }
  }

  /**
   * {@inheritDoc}
   */
  public function offsetUnset($offset) {
    $this->delete($offset);
    unset($this->proxy[$offset]);
    parent::offsetUnset($offset);
  }

  /**
   * {@inheritDoc}
   */
  public function rewind() {
    reset($this->proxy);
    $this->position = current($this->proxy);
  }

  /**
   * {@inheritDoc}
   */
  public function end() {
    end($this->proxy);
    $this->position = current($this->proxy);
  }

  /**
   * Removes a value from a particular backend.
   *
   * @param mixed $offset An array key.
   */
  abstract public function delete($offset);

  /**
   * Retrieves a value from a particular backend.
   *
   * @param mixed $offset An array key.
   * @return mixed A array value.
   */
  abstract public function get($offset);

  /**
   * Sets a value into a particular backend.
   *
   * @param mixed $offset An array key.
   * @param mixed $value An array value.
   * @return
   */
  abstract public function set($offset, $value);

}